package com.ppt.android.base.ktx

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.view.WindowManager
import androidx.core.content.ContextCompat
import com.ppt.android.base.utils.LogUtils
import com.ppt.android.base.utils.toast

/**
 * 设置当前 [Activity] 是否允许截屏操作
 *
 * @receiver [Activity]
 * @param isAllow Boolean 是否允许截屏
 * @author ZouJiaCai
 * @since 2023/6/25 11:00
 */
fun Activity.isAllowScreenCapture(isAllow: Boolean){
    if (isAllow){
        window?.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
    }else{
        window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
    }
}

/**检查精确定位权限*/
fun Context.isFineLocationPermissionGranted():Boolean{
    return ContextCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
}

/**检查模糊定位权限*/
fun Context.isCoarseLocationPermissionGranted(): Boolean {
    return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
}

/**
 * 调用第三方支付APP（微信或者支付宝）
 */
fun Activity.openPayThirdPartyApp(openType: String,openUrl: String){
    try {
        if (openUrl.isEmpty()){
            toast("openUrl = null")
            return
        }
        LogUtils.w("调用协议 openUrl",openUrl)
        when(openType){
            "protocol","webview" -> {
                val uri = Uri.parse(openUrl)
                //使用网页打开
                val intent = Intent()
                intent.action = "android.intent.action.VIEW"
                intent.data = uri
                startActivity(intent)
            }
            else -> {

            }
        }

    }catch (e: Exception){
        e.printStackTrace()
    }
}